import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import HttpRepositiry from '@/repositorys/HttpRepositiry';
import { cache } from '@/utils/utils';

const IS_DEBUG = process.env.NODE_ENV == 'development';
export type AccountType = { nick: string, sid: string, uid: string, expireTime?: number };

export const useAccount = defineStore('account', () => {
    const values = ref<AccountType[]>([]);
    const expireValues = ref<AccountType[]>([]);

    const accounts = computed(() => {

        return values.value;
    });

    const own = ref<{ nick: string, uid: string, sid: string, expireTime: number }[]>(cache.get('__taobao_account__') || []);

    const isOwn = computed(() => {
        if (IS_DEBUG) {
            return true;
        }
        return own.value.length >= 2;
    });

    const pullAccounts = async () => {
        const res = await HttpRepositiry.getAccounts();
        expireValues.value = [];
        if (res.status == 200) {
            values.value = res.data.body;
        }

    }

    const pullAccount = async (uid: string) => {
        const res = await HttpRepositiry.pullAccount({ uid });
        if (res.status == 200) {
            const user = res.data.body;
            const index = values.value.findIndex((item) => item.uid == user.uid);
            if (index > -1) {
                values.value.splice(index, 1, user);
            } else {
                values.value.unshift(user);
            }
        }
        return res.data.body;

    }


    const add = async (info: { nick: string, sid: string, uid: string }, isMe: number = 0) => {
        const res = await HttpRepositiry.pushAccounts({
            ...info,
            isMe
        });
        if (res.status != 200) {
            return;
        }
        const user = res.data.body;
        if (!isMe) {
            const index = values.value.findIndex((item) => item.uid == user.uid);
            if (index > -1) {
                values.value.splice(index, 1, user);
            } else {
                values.value.unshift(user);
            }
        }

        return user;
    }

    const addOwn = async (info: { nick: string, sid: string, uid: string }) => {
        const user = await add(info, 1);
        if (user) {
            const index = own.value.findIndex((item) => item.uid == user.uid);
            if (index > -1) {
                own.value.splice(index, 1);
            }
            own.value.push(user);
            cache.set('__taobao_account__', own.value);
        }

    }

    const delOwn = (index: number) => {
        own.value.splice(index, 1);
        cache.set('__taobao_account__', own.value);

    }

    const updateAccountStatusExt = (sid: string) => {
        const list = values.value;


        const item = list.find((item) => item.sid == sid);
        if (item) {
            expireValues.value.push(item);
            item.expireTime = 0;
        }

    }

    const init = () => {
        pullAccounts();
    }
    return { accounts, pullAccounts, add, updateAccountStatusExt, init, own, addOwn, isOwn, pullAccount, delOwn }
});


